package com.formina.engine;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

public class ModelInfo {

	public float[] Vertices;
	public float[] Normals;
	public short[] Indices;
	public float[] TextureCoords = new float[]{};
	public float Width = 0f;
	public float Height = 0f;
	public float Depth = 0f;
	
	protected int[] textures = new int[1];

	   public FloatBuffer vertexBuffer;
	   public FloatBuffer normalBuffer;
	   public FloatBuffer textureBuffer;
	   public ShortBuffer indexBuffer;
	
	public void Bind(){
		ByteBuffer byteBuf = ByteBuffer.allocateDirect(Vertices.length * 4);
	      byteBuf.order(ByteOrder.nativeOrder());
	      vertexBuffer = byteBuf.asFloatBuffer();
	      vertexBuffer.put(Vertices);
	      vertexBuffer.position(0);
	      
	      byteBuf = ByteBuffer.allocateDirect(Normals.length * 4);
	      byteBuf.order(ByteOrder.nativeOrder());
	      normalBuffer = byteBuf.asFloatBuffer();
	      normalBuffer.put(Normals);
	      normalBuffer.position(0);
	      
	      byteBuf = ByteBuffer.allocateDirect(TextureCoords.length * 4);
	      byteBuf.order(ByteOrder.nativeOrder());
	      textureBuffer = byteBuf.asFloatBuffer();
	      textureBuffer.put(TextureCoords);
	      textureBuffer.position(0);
	      
	      byteBuf = ByteBuffer.allocateDirect(Indices.length * 2);
	      byteBuf.order(ByteOrder.nativeOrder());
	      indexBuffer = byteBuf.asShortBuffer();
	      indexBuffer.put(Indices);
	      indexBuffer.position(0);
	}
	
	public static void loadTexture(GL10 gl,int texture, Context context) {
	      InputStream imagestream = context.getResources().openRawResource(texture);
	      Bitmap bitmap = null;
	      try {
	        
	    	  bitmap = BitmapFactory.decodeStream(imagestream);

	      }catch(Exception e){
	    	  
	      }finally {
	         //Always clear and close
	         try {
	        	 imagestream.close();
	        	 imagestream = null;
	         } catch (IOException e) {
	         }
	      }

	      gl.glGenTextures(1, Engine.Textures, 0);
	      gl.glBindTexture(GL10.GL_TEXTURE_2D, Engine.Textures[0]);
	      
	      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
	      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

	      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
	      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
	      
	      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
	      
	      bitmap.recycle();
	   }
}


